<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 日志
 *
 */
class Log_model extends CI_Model{
	 
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 加载日志信息
	 */
	function load_log_info($data){
		$select = "a.id,a.user_id,a.add_time,a.ip,a.info,b.adminname";
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		
		$_where = " where 1=1 and is_del=0 ";
		if(isset($data['where']['adminname']) && $data['where']['adminname']){
			$_where .= " and b.adminname = ".$data['where']['adminname'];
		}
		
		if(isset($data['where']['add_time']) && $data['where']['add_time']){
			$strtime = strtotime($data['where']['add_time'].' 00:00:00');
			$endtime = strtotime($data['where']['add_time'].' 23:59:59');
			$_where .= " and a.add_time >= ".$strtime;
			$_where .= " and a.add_time <= ".$endtime;
		}
		
		
		$sql_result = "select $select from rsh_log a inner join rsh_system_admin_user b on a.user_id = b.id $_where order by id desc";
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['log_list'] = $query->result_array();
		
		$sql_count = "select count(*) as count from rsh_log a $_where";
		$query = $this->db->query($sql_count);
		$data['log_count'] = $query->row()->count;
		return $data;
	}
	
	/**
	 * 添加日志
	 */
	function add_log($info){
		$admin_user = $this->session->userdata('admin_user');
		$data = array(
			'user_id' => $admin_user['user_id'],
			'add_time'=> time(),
			'ip' 	  => $_SERVER["REMOTE_ADDR"],
			'info'    => $info,
		);
		$this->db->insert('rsh_log', $data);
	}
	
	/**
	 * 删除日志
	 */
	function delete_log($id,$data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_log',$data);
		return $this->db->affected_rows();
	}
}
